﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using mshtml;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace SSOLoginApi
{
    /// <summary>
    /// 网格化平台
    /// </summary>
    public class GridManager : SSOLoginBase
    {
        public void OpenWin(string url, string subPage)
        {
            string consturl = "http://10.168.13.210/";

            byte[] result = Encoding.UTF8.GetBytes(this.UserPwd);    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            string md5pwd = BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本
            
            Process.Start("iexplore.exe", "http://10.168.13.210/systemAccessUsersAction.do?method=userLogin&userid=" + UserAccount + "&pwd=" + md5pwd);  //直接打开IE浏览器，打开指定页
            System.Threading.Thread.Sleep(1000);

            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
            SHDocVw.InternetExplorer NewBrowser = null;
            int tryCount = 0;
            while (tryCount < 30000)
            {
                tryCount++;
                for (int i = shellWindows.Count; i > 0; i--)
                {

                    SHDocVw.InternetExplorer Browser = (SHDocVw.InternetExplorer)shellWindows.Item(i);
                    if (Browser != null && Browser.LocationURL.StartsWith("http://10.168.13.210/"))
                    {
                        NewBrowser = Browser;
                        tryCount = 40000;
                        break;
                    }

                }
            }

            if (NewBrowser.LocationURL.StartsWith("http://10.168.13.210/"))
            {
                if (NewBrowser.Document is HTMLDocumentClass)
                {
                    NameValueCollection NVC = ParseUrl(NewBrowser.LocationURL);


                    if (subPage != null && subPage.Trim().Length > 0)
                    {
                        NewBrowser.Navigate(consturl + subPage, 0);

                    }

                }
            }

            //mainfirm.setAttribute("value", "/module?md=bh-recv-list");

            //SHDocVw.InternetExplorer webBrowser1 = (SHDocVw.InternetExplorer)shellWindows.Item(shellWindows.Count - 1);

            //webBrowser1.Navigate(url, ref objFlags, ref objTargetFrameName, ref objPostData, ref objHeaders);
            
            //webBrowser1.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(webBrowser1_DocumentComplete);
        }
    }
}
